var API_HOST = 'http://mall.jaxin.net';

/**
 * 封装微信的request
 */
module.exports = function request(url, data = {}, method = "GET") {
    return new Promise(function (resolve, reject) {
        wx.request({
            url: `${API_HOST}${url}`,
            data: data,
            method: method,
            header: {
                'Content-Type': 'application/json',
                'Authorization': `Bearer ${wx.getStorageSync('token') || ''}`
            },
            success: function (res) {
                if (res.statusCode != 200 || !res.data || res.data.code !== 200) {
                    wx.showToast({
                        title: res.message,
                        icon: 'error',
                        duration: 1200
                    });

                    return reject(res);
                }

                resolve(res.data);
            },
            fail: function (err) {
                wx.showToast({
                    title: '系统故障,暂无法获取商品信息',
                    icon: 'error',
                    duration: 1200
                })

                reject(err)
            }
        });
    });
}
